from app import db
from hashlib import md5


class Auth(db.Document):
    auth_id = db.StringField()
    password = db.StringField()
    priority = db.BooleanField()
    mata = {"collection": "auth", "strict": False}

    @classmethod
    def insert_account(cls, auth_id, password, priority):
        password = md5(password.encode('utf8')).hexdigest()
        auth = cls.objects(auth_id=auth_id).first()
        if auth:
            return None
        auth = Auth(auth_id=auth_id, password=password, priority=priority).save()
        return auth

    @classmethod
    def verify_password(cls, auth_id, password):
        auth = cls.objects(auth_id=auth_id).first()
        password = md5(password.encode('utf8')).hexdigest()
        if auth is None:
            print('auth is None')
            return False
        elif auth['password'] == password:
            return True
            print('password right')
        else:
            print('false password')
            return False

    @classmethod
    def get_by_auth_id(cls, auth_id):
        return cls.objects(auth_id=auth_id).first()
'''
db.auth.insertOne({
    auth_id: "123",
    password: "202cb962ac59075b964b07152d234b70",
    priority: true
})
'''
